﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using MM.DBUtility;
using MM.Common;

namespace WebMMS.Manager
{
    public partial class NotificationAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurUser"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;
                }
                if (!IsPostBack)
                {
                    Bincbl(ref cblUrl, "SELECT ID,'('+CAST(ID AS VARCHAR(50))+')'+Title AS Value FROM T_NotificationUrl");
                    cblUrl.Attributes.Add("onclick", "CheckSelect()");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #region UserControl Bind
        private void Bincbl(ref CheckBoxList cbl, string strSQL)
        {
            cbl.Items.Clear();
            cbl.DataSource = DbHelperSql.Query(strSQL).Tables[0];
            cbl.DataValueField = "ID";
            cbl.DataTextField = "Value";
            cbl.DataBind();
        }
        #endregion
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string strTitle = txtTitle.Text.Trim();
                if (string.IsNullOrEmpty(strTitle))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "标题不能为空！");
                    return;
                }
                string strMsg = txtMsg.Text.Trim();
                if (string.IsNullOrEmpty(strMsg))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "通知的消息不能为空！");
                    return;
                }
                string strUrl = string.Empty;
                for (int i = 0; i < cblUrl.Items.Count; i++)
                {
                    if (cblUrl.Items[i].Selected)
                        strUrl = cblUrl.Items[i].Value;
                }

                string strSQL = "INSERT INTO T_Notification(Title,Msg,UrlID) VALUES(@Title,@Msg,@UrlID)";
                List<SqlParameter> parameters = new List<SqlParameter>();
                parameters.Add(SqlHelper.CreateInParameter("@Title", SqlDbType.NVarChar, 500, strTitle));
                parameters.Add(SqlHelper.CreateInParameter("@Msg", SqlDbType.NVarChar, 4000, strMsg));
                parameters.Add(SqlHelper.CreateInParameter("@UrlID", SqlDbType.NVarChar, 50, strUrl));
                MM.DBUtility.DbHelperSql.ExecuteSql(strSQL, parameters.ToArray());
                MM.Common.Common.PageAlertAndRedirect(this.Page, "添加成功！", "Notification.aspx");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}